from algorithm.associate.apriori import Apriori
 


class AssociateGateway:
    def __init__(self):
         self.dataTable = [
             {"id": 0, "name": "Associate1", "size": 20, "detail": "test data"},
             {"id": 1, "name": "Associate2", "size": 2000, "detail": "Retail"}]
 
         self.dataType = [
             [{"attr": "id", "type": "Int", "enabled": True},
              {"attr": "itemset", "type": "String", "enabled": True}],
             [{"attr": "transaction No.", "type": "Int", "enabled": True},
              {"attr": "goods set", "type": "String", "enabled": True}]]

         self.data = ["AprioriTest.txt","retail.txt"]

         
         self.dataset = [
             {'itemset': '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 \n', 'id': '1'},
             {'itemset': '30 31 32 \n', 'id': '2'},
             {'itemset': '33 34 35 \n', 'id': '3'},
             {'itemset': '36 37 38 39 40 41 42 43 44 45 46 \n', 'id': '4'},
             {'itemset': '38 39 47 48 \n', 'id': '5'},
             {'itemset': '38 39 48 49 50 51 52 53 54 55 56 57 58 \n', 'id': '6'},
             {'itemset': '32 41 59 60 61 62 \n', 'id': '7'},
             {'itemset': '3 39 48 \n', 'id': '8'},
             {'itemset': '63 64 65 66 67 68 \n', 'id': '9'},
             {'itemset': '32 69 \n', 'id': '10'},
             {'itemset': '48 70 71 72 \n', 'id': '11'},
             {'itemset': '39 73 74 75 76 77 78 79 \n', 'id': '12'},
             {'itemset': '36 38 39 41 48 79 80 81 \n', 'id': '13'},
             {'itemset': '82 83 84 \n', 'id': '14'},
             {'itemset': '41 85 86 87 88 \n', 'id': '15'},
             {'itemset': '39 48 89 90 91 92 93 94 95 96 97 98 99 100 101 \n', 'id': '16'},
             {'itemset': '36 38 39 48 89 \n', 'id': '17'},
             {'itemset': '39 41 102 103 104 105 106 107 108 \n', 'id': '18'},
             {'itemset': '38 39 41 109 110 \n', 'id': '19'},
             {'itemset': '39 111 112 113 114 115 116 117 118 \n', 'id': '20'},
             {'itemset': '119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 \n', 'id': '21'},
             {'itemset': '48 134 135 136 \n', 'id': '22'},
             {'itemset': '39 48 137 138 139 140 141 142 143 144 145 146 147 148 149 \n', 'id': '23'},
             {'itemset': '39 150 151 152 \n', 'id': '24'},
             {'itemset': '38 39 56 153 154 155 \n', 'id': '25'},
             {'itemset': '48 156 157 158 159 160 \n', 'id': '26'},
             {'itemset': '39 41 48 \n', 'id': '27'},
             {'itemset': '161 162 163 164 165 166 167 \n', 'id': '28'},
             {'itemset': '38 39 48 168 169 170 171 172 173 \n', 'id': '29'},
             {'itemset': '32 39 41 48 174 175 176 177 178 \n', 'id': '30'},
             {'itemset': '32 38 39 47 48 179 180 181 182 183 \n', 'id': '31'},
             {'itemset': '39 184 185 186 \n', 'id': '32'},
             {'itemset': '36 38 41 48 140 187 188 \n', 'id': '33'}]
         
         
         self.result = [
             {'conf': 33, 'itemsetone': (38,), 'itemsettwo': (48, 39), 'sup': 21},             
             {'conf': 33, 'itemsetone': (39,48), 'itemsettwo': (41,), 'sup': 24},
             {'conf': 35, 'itemsetone': (39,41), 'itemsettwo': (38,), 'sup': 18},
             {'conf': 42, 'itemsetone': (41,), 'itemsettwo': (48, 39), 'sup': 24},
             {'conf': 46, 'itemsetone': (38,39), 'itemsettwo': (41,), 'sup': 13},
             {'conf': 51, 'itemsetone': (38,39), 'itemsettwo': (48,), 'sup': 13},
             {'conf': 55, 'itemsetone': (39,41), 'itemsettwo': (48,), 'sup': 18},
             {'conf': 74, 'itemsetone': (38,48), 'itemsettwo': (39,), 'sup': 9},
             {'conf': 78, 'itemsetone': (38,41), 'itemsettwo': (39,), 'sup': 8},
             {'conf': 79, 'itemsetone': (41,48), 'itemsettwo': (39,), 'sup': 12}]
         


    def getDataTable(self):
         return {"dataTable": self.dataTable}
 
    def getDataType(self, dataId):
         return {"dataType": self.dataType[dataId]}
    
    def getDataSet(self, dataId):
         
         filename = self.data[dataId]
         file = open(filename, "r")
         num = 0
         self.dataSet = []
         while 1:
             num += 1
             line = file.readline()
             if line == '':
                 break
             newDict = {}
             newDict["id"] = str(num)
             newDict["itemset"] = line 
             self.dataSet.append(newDict)
         
         return {"dataSet" : self.dataset} 

    def apriori(self, dataId, min_sup, min_conf):
         """
         ap = Apriori(self.data[dataId], min_sup, min_conf)
         result = ap.AprioriMine()
         self.result = []
         for rule in result:
             newDict = {}
             newDict["conf"] = rule[0]
             newDict["itemsetone"] = rule[1]
             newDict["itemsettwo"] = rule[2]
             if rule[3]>rule[4] :
                 newDict["sup"] = rule[4]
             else:
                 newDict["sup"] = rule[3]
             self.result.append(newDict)
         """
         return {"result": self.result}
 
